package com.qiwx.pigeon.activity.query;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.adapter.query.LayEggRecordAdapter;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.params.FertilizeParam;
import com.qiwx.pigeon.data.vo.CageProduceBean;
import com.qiwx.pigeon.data.vo.FertilizeBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.LayEggRecordPresenter;
import com.qiwx.pigeon.viewinterface.ILayEggRecordView;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.CustomGridItemDecoration;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-15 15:11
 * 下蛋记录
 **/
public class LayEggRecordActivity extends BaseMVPActivity<ILayEggRecordView, LayEggRecordPresenter> implements ILayEggRecordView {
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @BindView(R.id.rel_nodata_recycleview)
    RelativeLayout relNodataRecycleview;
    int days = 0;
    int pageNum = 1;//偏移量
    int pageSize = 20;//每页显示20条记录
    List<CageProduceBean> data = new ArrayList<>();
    LayEggRecordAdapter adapter;
    //    long today = 0;
    long unit_day = 1000 * 60 * 60 * 24;
    int status = 0;
    int c_number;
    int c_id;
    boolean isFresh;

    FertilizeParam fertilizeParam = new FertilizeParam();

    @Override
    public int getLayoutId() {
        return R.layout.activity_lay_egg_record;
    }

    @Override
    public void initAfterOnCreate() {
        initView();
        fertilizeParam = new FertilizeParam();
        fertilizeParam.setPageSize(pageSize);
        fertilizeParam.setC_id(c_id);
        fertilizeParam.setF_id(AppContext.getFarmId(this));
        fertilizeParam.setNumber(c_number);
        fertilizeParam.setPageNum(pageNum);
        loadData(true);
    }

    private void initView() {
        c_id = getIntent().getIntExtra("c_id", 0);
        c_number = getIntent().getIntExtra("c_number", 0);
        titleReturn.changeArrowLeft();
        titleContent.setText(c_number + "号鸽笼下蛋记录");

        String st1 = DateTimeUtils.timeStamp2DateTime(new Date().getTime());
        // today = DateTimeUtils.addTimeSuffix(st1, true);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.addItemDecoration(new CustomGridItemDecoration(1));
        adapter = new LayEggRecordAdapter(R.layout.adapter_lay_egg_record_list_item, data);
        recyclerview.setAdapter(adapter);
//        adapter.setiCageInterface((pos, c_id, medhid) -> {
//            updateListSelected(pos, c_id, medhid);
//        });

        xRefreshView.setHideFooterWhenComplete(true);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                loadData(true);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                pageNum++;
                loadData(false);
            }
        });


    }


    private void loadData(boolean isFresh) {
        this.isFresh = isFresh;
        if (isFresh) {
            pageNum = 1;
            data.clear();
        }
        fertilizeParam.setPageNum(pageNum);
        mPresenter.getLayEggRecordByPage(fertilizeParam);
    }


    @OnClick({R.id.title_return, R.id.txt_nodata})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.txt_nodata:
                break;
        }
    }

    @Override
    public LayEggRecordPresenter createPresenter() {
        return new LayEggRecordPresenter(new WeakReference(this));
    }

    @Override
    public void onLayEggRecordSuccess(PageInfo<CageProduceBean> result) {
        if (result.getList() != null && result.getList().size() > 0) {
            this.data.addAll(result.getList());
        }
        if (!isFresh && !result.isHasNextPage()) {
            MyToast.showLong("没有更多数据了");
        }
        if (isFresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();
        }
        if (!result.isHasNextPage()) {
            xRefreshView.setPullLoadEnable(false);
        } else {
            xRefreshView.setPullLoadEnable(true);
        }
        adapter.notifyDataSetChanged();
        if (data.size() == 0) {
            linNodata.setVisibility(View.VISIBLE);
            xRefreshView.setVisibility(View.GONE);
        } else {
            linNodata.setVisibility(View.GONE);
            xRefreshView.setVisibility(View.VISIBLE);

        }


    }

    @Override
    public void onFailed(String error) {

    }
}
